#!/bin/bash
# =================================================================
# Title:        滇池学院校园网络Linux/Mac认证脚本
# Description:  用于对滇池学院校园网络进行快速验证（直接可以绕过服务号扫码授权）
#               请用于合法用途，后果自负
# Author:       Penge，有问题联系QQ：631759716
# Version:      1.0
# =================================================================
TARGET_URL="http://172.17.2.252/0.htm"
MD5_PREFIX="2"
MD5_SUFFIX="12345678"
UPASS_SUFFIX="123456782"
echo "正在检查是否已接入校园网 ..."
http_status=$(curl -s -o /dev/null -w "%{http_code}" --head --connect-timeout 5 "${TARGET_URL}")
if [[ "$http_status" -ne 200 ]]; then
    echo -e "\n\033[0;31m当前网络环境没有接入校园网\033[0m" >&2
    echo -e "\033[0;33m请检查WIFI或者网线是否是加入校园网\033[0m" >&2
    exit 1
else
    echo -e "\033[0;32m检测到校园网环境，OK！\033[0m\n"
fi
read -p "请输入账号: " username
read -p "请输入密码: " password
echo
if [[ -z "$username" ]] || [[ -z "$password" ]]; then
    echo -e "\n\033[0;31m错误：账号和密码不能为空。\033[0m" >&2
    exit 1
fi
string_to_hash="${MD5_PREFIX}${password}${MD5_SUFFIX}"
md5_hash=$(echo -n "${string_to_hash}" | md5sum | awk '{print \$1}')
upass="${md5_hash}${UPASS_SUFFIX}"
post_data="DDDDD=${username}&upass=${upass}&R1=0&R2=1&R3=1&R6=0&para=00&0MKKey=123456&v6ip="
echo "正在构造进行校园网认证"
curl -X POST "${TARGET_URL}" \
    -H "Connection: keep-alive" \
    -H "Cache-Control: max-age=0" \
    -H "Origin: http://172.17.2.252" \
    -H "Upgrade-Insecure-Requests: 1" \
    -H "Content-Type: application/x-www-form-urlencoded; charset=UTF-8" \
    -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36" \
    -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3" \
    -H "Referer: http://172.17.2.252/0.htm" \
    -H "Accept-Encoding: gzip, deflate" \
    -H "Accept-Language: zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,zh-TW;q=0.6" \
    --data "${post_data}" \
    --insecure

echo -e "接入校园网成功！祝你上网愉快！\n。"